home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9390 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.8 KB  |  217 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!news1!ts00-and-17
  3. From: dlmiller@iquest.net (Doug & Rose Miller)
  4. Subject: Re: Can C save and retreive from disk?
  5. X-Nntp-Posting-Host: ts00-and-17.iquest.net
  6. Message-ID: <Do2tDq.EA7@iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Network Services
  9. X-Newsreader: News Xpress Version 1.0 Beta #2.1
  10. References: <4hpd14$hel@lantana.singnet.com.sg>
  11. Date: Sun, 10 Mar 1996 23:56:46 GMT
  12.  
  13. Teddy Bear <s7700038@singnet.com.sg> wrote:
  14. +This is a multi-part message in MIME format.
  15. +
  16. +---------------------------------300002593617914
  17. +Content-Transfer-Encoding: 7bit
  18. +Content-Type: text/plain; charset=us-ascii
  19. +
  20. +Can anyone teach me or refer me or help in any way?
  21. +I need to save to disk and retrieve from disk
  22. +Can anyone help me
  23. +The prog goes
  24. +
  25. +---------------------------------300002593617914
  26. +Content-Transfer-Encoding: 7bit
  27. +Content-Type: text/plain
  28. +
  29. +/* This program simply enable the user to enter the particular(s). It will
  30. +   then sort the name in an alphabetical order and display the particular(s)
  31. +   on the screen. It also ensures that the user is able to add and delete
  32. +   the particular(s). */
  33. +
  34. +#include <stdio.h>
  35. +#include <alloc.h>
  36. +#include <conio.h>
  37. +#include <string.h>
  38. +#include <stdlib.h>
  39. +#define TRUE  1
  40. +#define FALSE 0
  41. +
  42. +typedef struct data{
  43. +    int age;
  44. +    char name[40];
  45. +    char address[100];
  46. +    struct data *next;
  47. +}dataelm;
  48. +typedef dataelm* dataptr;
  49. +
  50. +void createdata(dataptr *ptr_to_head, dataptr head);
  51. +int  countlink(dataptr head);
  52. +void sortlink(dataptr head);
  53. +dataptr searchlink(dataptr head, char *wanted);
  54. +void display(dataptr head);
  55. +void save(dataptr headptr, char file[20]);
  56. +void read(char file[20]);
  57. +dataptr clearlink(dataptr head);
  58. +dataptr addnode(dataptr head);
  59. +dataptr delnode(dataptr head);
  60. +void check(dataptr head);
  61. +
  62. +int main()
  63. +{
  64. +    char *sname, option;
  65. +    int checker = 0;
  66. +    dataptr ptr, *ptr_to_head, head, tail;
  67. +
  68. +    head = NULL;     /* assigns head with NULL, starting of link list */
  69. +    option = '1';
  70. +    do
  71. +    {
  72. +    clrscr();
  73. +    printf("\n\nÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ  MAIN MENU  ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ\n");
  74. +    printf("\n1) Adding of particular(s).\n");
  75. +    printf("2) Adding of new particular(s).\n");
  76. +    printf("3) Deleting of current particular(s).\n");
  77. +    printf("4) Display all the particular(s) entered.\n");
  78. +    printf("5) Exit the program.\n");
  79. +    printf("\nPlease enter your choice : ");
  80. +    if(option < '0' || option > '6')
  81. +        printf("\nInvaild option. Please re-enter.");
  82. +    option = getche();
  83. +    switch(option)
  84. +    {
  85. +        case '1' : if(checker != 0)
  86. +               {
  87. +               printf("\a");
  88. +               printf("\n\nYou had create a link list previously.");
  89. +               printf("\nIf you proceed, the previous data will be lost.");
  90. +               printf("\n\nEnter a 'Y' to proceed.");
  91. +               printf("\nEnter ESC to return to main menu ");
  92. +               if((toupper(getch())) != 'Y')
  93. +                   break;
  94. +               else
  95. +                   clearlink(head);
  96. +               }
  97. +               createdata(ptr_to_head, NULL);
  98. +               head = *ptr_to_head;
  99. +               sortlink(head);
  100. +               display(head);
  101. +               checker = 1;
  102. +               break;
  103. +        case '2' : if(checker != 1)
  104. +               {
  105. +               printf("\a");
  106. +               printf("\n\nYou have not yet create a link list.");
  107. +               printf("\nPlease choose option '1'.");
  108. +               printf("\n\nPress any key to continue.");
  109. +               getch();
  110. +               break;
  111. +               }
  112. +               head = addnode(head);
  113. +               checker = 1;
  114. +               display(head);
  115. +               break;
  116. +        case '3' : if(checker!=1)
  117. +               {
  118. +               printf("\a");
  119. +               printf("\n\nYou have not yet create a link list.");
  120. +               printf("\nPlease choose option '1'.");
  121. +               printf("\n\nPress any key to continue.");
  122. +               getch();
  123. +               break;
  124. +               }
  125. +               display(head);
  126. +               head = delnode(head);
  127. +               display(head);
  128. +               if(head == NULL)
  129. +               checker = 0;
  130. +               break;
  131. +        case '4' : display(head);
  132. +               break;
  133. +        case '5' : printf("\n\nHave a nice day. BYE!\n");
  134. +               printf("Press any key to end...");
  135. +               getch();
  136. +               exit(0);
  137. +    }
  138. +    }
  139. +    while(option != '5');
  140. +    return 0;
  141. +}
  142. +
  143. +void createdata(dataptr *ptr_to_head, dataptr head)
  144. +/* create a link of the telephone list */
  145. +{
  146. +    dataptr temp, last;
  147. +    char ans;
  148. +    int elm_size = sizeof(dataelm);
  149. +    do
  150. +    {
  151. +    temp = (dataptr)malloc(elm_size);
  152. +    clrscr();
  153. +    printf("\n\nÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ  PARTICULAR CREATING  ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ\n");
  154. +    printf("\nEnter name to be added   : ");
  155. +    gets(temp->name);
  156. +    printf("Enter Age                : ");
  157. +    scanf("%d",&temp->age);
  158. +    fflush(stdin);
  159. +    printf("Enter Address            : ");
  160. +    gets(temp->address);
  161. +
  162. +    if(head == NULL)
  163. +    {
  164. +        head = temp;      /* assigns the address of dataptr to head */
  165. +        last = temp;
  166. +    }
  167. +    else
  168. +    {
  169. +        last->next=temp;
  170. +        last = temp;
  171. +    }
  172. +    printf("\nAny more you would like to enter ? <Y/N> : ");
  173. +    ans=toupper(getch());
  174. +    }
  175. +    while(ans != 'N');
  176. +    last->next = NULL;
  177. +    *ptr_to_head = head;
  178. +}
  179. +
  180. +void sortlink(dataptr head)
  181. +/* sort the link in an alphabetical order */
  182. +{
  183. +    dataptr front, back;
  184. +    struct data temp;
  185. +    int pass, cnt, condition;
  186. +    int element;
  187. +    element = countlink(head);
  188. +    pass = 0;
  189. +    do
  190. +    {
  191. +    pass++;
  192. +    condition = TRUE;
  193. +    front = head->next;
  194. +    back  = head;
  195. +    for(cnt = 0; cnt < element - pass; cnt++)
  196. +    {
  197. +        /* compare the character */
  198. +        if((strcmp((back->name), (front->name))) > 0)
  199. +        {
  200. +        condition = FALSE;
  201. +        strcpy(temp.name, back->name);
  202. +        temp.age = back->age;
  203. +        strcpy(temp.address, back->address);
  204. +        strcpy(back->name, front->name);
  205. +        back->age = front->age;
  206. +        strcpy(back->address, front->address);
  207. +        strcpy(front->name, temp.name);
  208. +        front->age = temp.age;
  209. +        strcpy(front->address, temp.address);
  210. +        }
  211. +        back = front;
  212. +        front = front->next;
  213. +    }
  214. +
  215. + ... blah blah blah ...
  216. Try debugging your program.
  217.